<template>
    <div class="home">
        <el-container class="home-container">
            <el-header>
                <el-row>
                    <el-col :span="4">
                        <p class="system-name">{{ $constant.appName }}</p>
                    </el-col>
                    <el-col :offset="12" :span="8" style="min-width: 150px">
                        <el-dropdown style="float: right; margin: 20px 10px">
                            <span class="el-dropdown-link" style="color: #fff; cursor: pointer">
                                {{ username }} &nbsp;&nbsp;<i class="fa fa-caret-down fa-1x"></i>
                            </span>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item @click.native="editPasswordDialog = true">修改密码</el-dropdown-item>
                                <el-dropdown-item @click.native="logout()">退出系统</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                        <el-avatar shape="square" :src="avatar" style="margin: 10px; float: right"></el-avatar>

                    </el-col>
                    <!-- 修改密码 dialog -->
                    <el-dialog title="修改密码" :visible.sync="editPasswordDialog" width="30%" :before-close="closeEditPassword" :close-on-click-modal="false">
                        <el-form ref="editPasswordForm" :rules="editPasswordRule" class="demo-form-inline" :model="editPasswordForm" label-width="90px">
                            <el-form-item label="原密码:" prop="oldPassword">
                                <el-input v-model="editPasswordForm.oldPassword" placeholder="请输入原密码" />
                            </el-form-item>
                            <el-form-item label="新密码:" prop="newPassword">
                                <el-input v-model="editPasswordForm.newPassword" placeholder="请输入新密码" />
                            </el-form-item>
                            <el-form-item label="确认密码:" prop="confirmPassword">
                                <el-input v-model="editPasswordForm.confirmPassword" placeholder="请确认密码" />
                            </el-form-item>
                            <el-form-item>
                                <el-button size="small" @click="closeEditPassword()">取 消</el-button>
                                <el-button size="small" type="primary" @click="editPassword()">保 存</el-button>
                            </el-form-item>
                        </el-form>
                    </el-dialog>
                </el-row>
            </el-header>
            <el-container style="overflow: auto">
                <el-aside>
                    <div class="toggle-button" @click="isColapse = !isColapse">
                        <i v-if="isColapse" class="el-icon-s-unfold"></i>
                        <i v-if="!isColapse" class="el-icon-s-fold"></i>
                    </div>
                    <el-menu router :default-active="activePath" class="el-menu-vertical-demo" :collapse="isColapse">
                        <el-menu-item index="/index" @click="saveActiveNav('/index')">
                            <i class="el-icon-house"></i>
                            <span slot="title">首页</span>
                        </el-menu-item>
                        <el-menu-item index="/ns" @click="saveActiveNav('/ns')">
                            <i class="el-icon-menu"></i>
                            <span>命名空间</span>
                        </el-menu-item>
                        <el-menu-item index="/ds" @click="saveActiveNav('/ds')">
                            <i class="el-icon-coin"></i>
                            <span slot="title">数据源管理</span>
                        </el-menu-item>
                        <el-submenu index="1">
                            <template slot="title">
                                <i class="el-icon-document"></i>
                                <span>在线SQL</span>
                            </template>
                            <el-menu-item index="/mapper" @click="saveActiveNav('/mapper')">
                                <i class="el-icon-postcard"></i>
                                <span slot="title">Mapper</span>
                            </el-menu-item>
                            <el-menu-item index="/mapper/sql" @click="saveActiveNav('/mapper/sql')">
                                <i class="el-icon-edit-outline"></i>
                                <span slot="title">Sql</span>
                            </el-menu-item>
                            <el-menu-item index="/sql/log" @click="saveActiveNav('/sql/log')">
                                <i class="el-icon-notebook-1"></i>
                                <span slot="title">日志管理</span>
                            </el-menu-item>
                        </el-submenu>
                        <el-submenu index="2">
                            <template slot="title">
                                <i class="el-icon-camera-solid"></i>
                                <span>代码生成器</span>
                            </template>
                            <el-menu-item index="/template" @click="saveActiveNav('/template')">
                                <i class="el-icon-setting"></i>
                                <span slot="title">模板管理</span>
                            </el-menu-item>
                            <el-menu-item index="/generator" @click="saveActiveNav('/generator')">
                                <i class="el-icon-printer"></i>
                                <span slot="title">生成器</span>
                            </el-menu-item>
                        </el-submenu>
                        <el-submenu index="3">
                            <template slot="title">
                                <i class="el-icon-setting"></i>
                                <span>系统配置</span>
                            </template>
                            <el-menu-item index="/sys/db" @click="saveActiveNav('/sys/db')">
                                <i class="el-icon-coin"></i>
                                <span slot="title">数据库</span>
                            </el-menu-item>
                        </el-submenu>
                        <el-menu-item index="/user" @click="saveActiveNav('/user')">
                            <i class="el-icon-user"></i>
                            <span slot="title">用户管理</span>
                        </el-menu-item>
                        <el-submenu index="4">
                            <template slot="title">
                                <i class="el-icon-document-copy"></i>
                                <span>集群管理</span>
                            </template>
                            <el-menu-item index="/clusterManagement" @click="saveActiveNav('/clusterManagement')">
                                <i class="el-icon-postcard"></i>
                                <span slot="title">节点列表</span>
                            </el-menu-item>
                        </el-submenu>
                    </el-menu>
                </el-aside>
                <el-container>
                    <el-main>
                        <!-- 面包屑 -->
                        <Breadcrumb />
                        <!-- 主要内容 -->
                        <router-view></router-view>
                    </el-main>
                    <el-footer>Copyright © 2023 {{ $constant.officialAccount }}</el-footer>
                </el-container>
            </el-container>
        </el-container>
    </div>
</template>
<script>
    import Breadcrumb from '../components/Breadcrumb.vue'
    import { userUpdatePwd } from '../api/user'
    import constantJs from '../assets/js/constant'
    export default {
        components: {
            Breadcrumb
        },
        data() {
            return {
                avatar: require('@/assets/img/avator.jpg'),
                username: '',
                isColapse: false,
                //activePath当前激活的路由路径 ，应该修改为全局变量 使用vueXs来操作
                //activePath: '',
                editPasswordDialog: false,
                editPasswordForm: {
                    oldPassword: '',
                    newPassword: '',
                    confirmPassword: ''
                },
                editPasswordRule: {
                    oldPassword: [
                        { required: true, message: '请输入原密码', trigger: 'blur' }
                    ],
                    newPassword: [
                        { required: true, message: '请输入新密码', trigger: 'blur' }
                    ],
                    confirmPassword: [
                        { required: true, message: '请确认新密码', trigger: 'blur' }
                    ]
                },
                // 修改密码
                editPassword() {
                    this.$refs.editPasswordForm.validate(async (valid) => {
                        if (!valid) return
                        if (this.editPasswordForm.newPassword !== this.editPasswordForm.confirmPassword) {
                            return this.$message.error('两次密码不正确，请重新输入！')
                        }
                        // 请求接口
                        userUpdatePwd(localStorage.getItem(constantJs.authUserHeader), this.editPasswordForm.oldPassword, this.editPasswordForm.newPassword).then(res => {
                            this.$message.success('密码修改成功，请重新登录！')
                            localStorage.clear()
                            this.$router.push('/login')
                        })
                    })
                }
            }
        },
        mounted() {
            this.$bus.$on('changeView', (url, queryParams) => {
                this.$commonJs.changeView(url, queryParams)
                this.saveActiveNav(url)
            })
            this.username = localStorage.getItem(constantJs.authUserHeader) ? localStorage.getItem(constantJs.authUserHeader) : 'wtf'
        },
        beforeDestroy() {
            this.$bus.off('changeView')
        },
        computed: {
            activePath() {
                return this.$store.state.activePath
            }
        },
        methods: {
            saveActiveNav(activePath) {
                //localStorage.setItem('activePath', activePath)
                this.$store.dispatch('changeActivePath', activePath)
            },
            logout() {
                this.$confirm('确定要退出系统吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                    .then(() => {
                        // 清除缓存
                        localStorage.clear()
                        this.$router.push('/login')
                    })
                    .catch(() => {
                        return false
                    })
            },
            // 取消关闭密码
            closeEditPassword() {
                this.editPasswordDialog = false
                // 坑：resetFields 方法只能重置带有 props 属性的元素
                this.$refs.editPasswordForm.resetFields()
            }
        }
    }
</script>

<style scoped>
.home-container {
    position: absolute;
    height: 100%;
    top: 0px;
    left: 0px;
    width: 100%;
    background: #f2f3f5;
}
.el-header {
    background: rgb(55, 60, 100);
    padding: 0 10px;
    overflow: hidden;
}

.el-aside {
    background: white;
    width: auto !important;
}

.el-main {
    background-color: #d9e0e7;
    color: #333;
    text-align: center;
}

.toggle-button {
    background-color: #d9e0e7;
    font-size: 18px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
    color: black;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
}

.el-footer {
    color: rgb(55, 60, 100);
    text-align: center;
    line-height: 60px;
}

.system-name {
    color: #fff;
    font-size: 18px;
}

.el-menu-item.is-active {
    color: #fff !important;
    font-size: 15px;
    font-weight: bold;
    background-color: rgb(55, 60, 100) !important;
    border-radius: 2px;
    height: 50px;
    line-height: 50px;
    box-sizing: border-box;
    margin: 2px 5px 0px 2px;
}
</style>
